package com.aaron.messenger.data;

import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;

public class MessengerDatabase {
	public static class FileTransferHistroyTable implements BaseColumns {
		public static final String TIME = "time";
		public static final String RECEIVER_NAME = "name";
		public static final String RECEIVER_IP = "ip";
		public static final String RECEIVER_MAC = "mac"; 
		public static final String SAVEPATH = "path";
	}
	
	public static class ChatHistroyTable implements BaseColumns {
		public static final String TIME = "time";
		public static final String MAC = "mac";
		public static final String IP = "ip";
		public static final String NAME = "name";
		public static final String ISOUT = "isout";
		public static final String CONTENT = "content";
		public static final String ISGESTURE = "isgesture";
	}
	
	private static final String DBNAME = "histroy";
	private static final String FILETRANSFER_HISTROY_TABLE = "filetransfer";
	private static final String CHAT_HISTROY_TABLE = "chat";
	
	private SQLiteDatabase mDB;
	
	public void insertFileTransferHistroy(String name, String ip, String mac, String path) {
		ContentValues value = new ContentValues();
		value.put(FileTransferHistroyTable.TIME, new Date().getTime());
		value.put(FileTransferHistroyTable.RECEIVER_NAME, name);
		value.put(FileTransferHistroyTable.RECEIVER_IP, ip);
		value.put(FileTransferHistroyTable.RECEIVER_MAC, mac);
		value.put(FileTransferHistroyTable.SAVEPATH, path);
		mDB.insert(FILETRANSFER_HISTROY_TABLE, FileTransferHistroyTable.RECEIVER_IP, value);
	}
	
	public void insertChatHistroy(String name, String ip, String mac, boolean isout, boolean isgesture) {
		ContentValues value = new ContentValues();
		value.put(ChatHistroyTable.TIME, new Date().getTime());
		value.put(ChatHistroyTable.NAME, name);
		value.put(ChatHistroyTable.IP, ip);
		value.put(ChatHistroyTable.MAC, mac);
		value.put(ChatHistroyTable.ISGESTURE, isgesture);
		value.put(ChatHistroyTable.ISOUT, isout);
		mDB.insert(CHAT_HISTROY_TABLE, ChatHistroyTable.IP, value);
	}
	
	public void cleanFileTransferHistroy() {
		mDB.execSQL("delete * from "+FILETRANSFER_HISTROY_TABLE);
	}
	
	public void cleanChatHistroy(String target) {
		mDB.execSQL("delete * from "+CHAT_HISTROY_TABLE+"  where "+ChatHistroyTable.MAC+" = "+target);
	}
	
	public void cleanAllChatHistroy() {
		mDB.execSQL("delete * from "+CHAT_HISTROY_TABLE);
	}
	
	public void closeDB() {
		mDB.close();
		mDB = null;
	}
	
	private MessengerDatabase(Context context) {
		mDB = context.openOrCreateDatabase(DBNAME, 0, null);
		StringBuilder sb = new StringBuilder("create table if not exists ");
		sb.append(FILETRANSFER_HISTROY_TABLE)
		.append(" (")
		.append(FileTransferHistroyTable._ID)
		.append("  Integer auto_increment,")
		.append(FileTransferHistroyTable.TIME)
		.append("  Long,")
		.append(FileTransferHistroyTable.RECEIVER_NAME)
		.append("  String,")
		.append(FileTransferHistroyTable.RECEIVER_IP)
		.append("  String,")
		.append(FileTransferHistroyTable.RECEIVER_MAC)
		.append("  String,")
		.append(FileTransferHistroyTable.SAVEPATH)
		.append("  String)");
		mDB.execSQL(sb.toString());
		
		sb = new StringBuilder("create table if not exists ");
		sb.append(CHAT_HISTROY_TABLE)
		.append(" (")
		.append(ChatHistroyTable._ID)
		.append("  Integer auto_increment,")
		.append(ChatHistroyTable.TIME)
		.append("  Long,")
		.append(ChatHistroyTable.MAC)
		.append("  String,")
		.append(ChatHistroyTable.IP)
		.append("  String,")
		.append(ChatHistroyTable.NAME)
		.append("  String,")
		.append(ChatHistroyTable.ISOUT)
		.append("  Boolean,")
		.append(ChatHistroyTable.CONTENT)
		.append("  String,")
		.append(ChatHistroyTable.ISGESTURE)
		.append("  Boolean)");
		mDB.execSQL(sb.toString());
	}
	
	private static MessengerDatabase sInstance;
	
	public synchronized static MessengerDatabase getDB(Context context) {
		if(sInstance == null) {
			sInstance = new MessengerDatabase(context);
		}
		return sInstance;
	}
}
